<cfcomponent output="false">
	<cfset $ = application.helpers />
	
	<!--- --->
	
	<cffunction name="getUser" access="public" output="true" returntype="query">
		<cfargument name="email" required="false" default="" />
		<cfargument name="userID" required="false" default="" />
		
		<cfreturn application.g.craftivityGateway.getUser(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getUserData" access="public" output="true" returntype="query">
		<cfargument name="email" required="false" default="" />
		<cfargument name="userID" required="false" default="" />
		
		<cfreturn application.g.craftivityGateway.getUserData(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
		
	<cffunction name="getUserCount" access="public" output="true" returntype="query">
		
		<cfreturn application.g.craftivityGateway.getUserCount(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getBusiness" access="public" output="true" returntype="query">
		<cfargument name="businessID" required="false" default="" />
		<cfargument name="userID" required="false" default="" />

		<cfreturn application.g.craftivityGateway.getBusiness(argumentCollection=arguments) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="setLoggedInUser" access="public" output="true" returntype="struct">
		<cfargument name="userID" required="false" default="" />

		<cfset var local = {} />
		
		<cfset local.user = application.g.craftivityGateway.getUser(userID=arguments.userID) />
		
		<!--- set request user values --->
		<cfset application.session.pushUserToSession(local.user) />
		
		<!--- save cookie with email, for future login --->
		<cfcookie name="c63" expires="never" value="#$.format.encrypt(local.user.email)#" />
		
		<cfreturn request.session.user />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveUser" access="public" output="true" returntype="string">
		<cfargument name="userID" required="false" default="" />
		<cfargument name="first_name" required="true" />
		<cfargument name="middle_name" required="true" />
		<cfargument name="last_name" required="true" />
		<cfargument name="bio" required="true" />
		<cfargument name="businessID" required="False" default="" />

		<cfset var local = {} />
		
		<cfset local.id = $.database.save(
			table="user",
			id=arguments.userID,
			first_name=arguments.first_name,
			middle_name=arguments.middle_name,
			last_name=arguments.last_name,
			bio=arguments.bio,
			business_id=arguments.businessID
		) />
				
		<!--- invalidate cache on this user --->
		<cfset local.user = $.run.it('craftivityService.getUser',{userID=request.session.user.id,invalidateCache=true}) />
		
		<!--- update request with user values --->
		<cfset application.session.pushUserToSession(local.user) />
		
		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveBusiness" access="public" output="true" returntype="string">
		<cfargument name="businessID" required="false" default="" />
		<cfargument name="name" required="true" />
		<cfargument name="stateID" required="true" />
		<cfargument name="website" required="true" />
		<cfargument name="description" required="true" />
		<cfargument name="timezone" required="true" />
		<cfargument name="dst" required="false" default="false" />
		<cfargument name="continent" required="true" />

		<cfset var local = {} />
		
		<cfset local.originalTimezone = request.session.user.timezone />
		
		<cftransaction>
			<cfset arguments.name = left(arguments.name,100) />
			<cfif arguments.name eq "">
				<cfset arguments.name = "---" />
			</cfif>
		
			<cfset local.id = $.database.save(
				table="business",
				id=arguments.businessID,
				name=arguments.name,
				state_ID=arguments.stateID,
				website=arguments.website,
				description=arguments.description,
				timezone=arguments.timezone,
				continent=arguments.continent
			) />
			
			<cfset $.run.it('craftivityService.setLoggedInUser',{userID=request.session.user.id}) />
			
			<!--- shift events by timezone change --->
			<cfif request.session.user.timezone neq arguments.timezone and local.originalTimezone neq "" and arguments.timezone neq "">
				<cfset $.run.it('calendarService.updateEventsByTimezoneChange',{before=local.originalTimezone,after=arguments.timezone}) />
			</cfif>
		
		</cftransaction>
		
		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getTodoList" access="public" output="true" returntype="array" hint="array of structs">

		<cfset var local = {} />
		
		<cfset local.user = $.run.it('craftivityService.getUser', {userID=request.session.user.id}) />
		<cfset local.business = $.run.it('craftivityService.getBusiness', {userID=local.user.id}) />
	
		<cfset local.todo = []>
		
		<!--- 0 quick tour --->
		<cfif true>
			<cfset local.item = {} />
			<cfsavecontent variable="local.item.text">
				Welcome to Craftivity! Click <b>2 Minute Tour</b> to get aquainted with the interface.
			</cfsavecontent>
			<cfset local.item.id = "setup_your_business">
			<cfset arrayAppend(local.todo,local.item) />
		</cfif>
		
		<!--- 1 set up business --->
		<cfif local.user.business_id eq ''>
			<cfset local.item = {} />
			<cfsavecontent variable="local.item.text">
				Welcome to the Craftivity Dashboard! Click <b>Set Up Your Business</b> to get started.
			</cfsavecontent>
			<cfset local.item.id = "setup_your_business">
			<cfset arrayAppend(local.todo,local.item) />
		</cfif>
		
		<!--- 2 tag business --->
		
		<!--- 3 do a worksheet --->
		
		<!--- 4 do a set up goals --->
				
		<!--- 5 do a set up milestones --->
		
		<!--- 6 do a set up action items --->
		
		<!--- 7 set up workweek --->
		
		<!--- 8 set up social --->
		
		<cfreturn local.todo />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveLocationTimezone" access="public" output="true" returntype="string">
		<cfargument name="businessID" required="false" default="" />
		<cfargument name="stateID" required="true" />
		<cfargument name="timezone" required="true" />
		<cfargument name="name" required="true" />
		<cfargument name="dst" required="false" default="false" />

		<cfset var local = {} />
		
		<cfset local.originalTimezone = request.session.user.timezone />
		
		<cfif arguments.dst>
			<cfset arguments.dst = 1 />
		<cfelse>
			<cfset arguments.dst = 0 />
		</cfif>

		<cfif arguments.businessID eq "">
			<cfset arguments.name = "Unnamed Business" />
		</cfif>
		
		<cftransaction>
		
			<cfset local.id = $.database.save(
				table="business",
				id=arguments.businessID,
				name=arguments.name,
				state_ID=arguments.stateID,
				timezone=arguments.timezone,
				continent=arguments.continent,
				dst=arguments.dst
			) />
			
			<cfset $.database.save(
				table="user",
				id=request.session.user.id,
				business_id=local.id
			) />
				
			<!--- invalidate cache on this user --->
			<cfset $.run.it('craftivityService.getUser',{userID=request.session.user.id,invalidateCache=true}) />
			
			<cfset $.run.it('craftivityService.setLoggedInUser',{userID=request.session.user.id}) />
			
			<!--- shift events by timezone change --->
			<cfif request.session.user.timezone neq arguments.timezone>
				<cfset $.run.it('calendarService.updateEventsByTimezoneChange',{before=local.originalTimezone,after=arguments.timezone}) />
			</cfif>
			
		</cftransaction>
		
		<cfreturn local.id />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="addSignupEmail" access="public" output="true" returntype="string">
		<cfargument name="email" required="false" default="" />
		<cfargument name="code" required="false" default="" />

		<cfset var local = {} />
		<cfset local.valid = true />
		
		<!--- see if valid --->
		<cfif len(arguments.email) eq 0>
			<cfreturn 'No Email Entered' />
		</cfif>
		
		<cfif listLen(arguments.email,'@') neq 2>
			<cfreturn 'Invalid Email Format (missing @)'/>
		</cfif>
		
		<cfif listLen(listGetAt(arguments.email,2,'@'),'.') neq 2>
			<cfreturn 'Invalid Email Format (missing domain)'/>
		</cfif>
		
		<!--- see if exists --->
		<cfquery name="local.emailCheck" datasource="#$.site.getVar('datasource')#">
			select hex(id) as id
			from signup_email
			where active = 1
			and email = #$.database.string(arguments.email)#
		</cfquery>
		
		<cfif local.emailCheck.recordcount gt 0>
			<cfreturn 'Email Exists' />
		</cfif>
		
		<!--- save email --->
		<cfset local.result = $.database.save(
			table="signup_email",
			id="",
			code=arguments.code,
			email=arguments.email		
		) />
		
		<!--- send to mailchimp --->
		<cfset local.chimp = $.run.http('https://us1.api.mailchimp.com/2.0/lists/subscribe',{
			apikey='88b092f2c0eb047a3c01d5876aef54ac-us1',
			id='a764d2810d',
			email={email=arguments.email}
		},'POST','','application/json','mailchimp signup') />
		
		<cfreturn "" />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getDateTime" access="public" output="true" returntype="string">
		
		<cfreturn dateTimeFormat($.date.applyLocalization(now()),'mm/dd/yyyy HH:nn:ss') />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="activateAccount" access="public" output="true" returntype="string">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />

		<cfset local.result = -1 /> <!--- invalid --->
		
		<cfif arguments.userID neq "">
			<!--- test for the userID account --->
			<cfquery name="local.getUser" datasource="#$.site.getVar('datasource')#">
				select 
				hex(user.id) as id,
				email,
				password
				from user
				where user.active = 0
				and user.id = #$.database.uuid(arguments.userID)#
			</cfquery>
			
			<cfif local.getUser.recordcount eq 0>
				<cfset local.result = 2 /> <!--- no user --->
			<cfelse>
				<!--- reactivate the user --->
				<cfquery name="local.reactivateUser" datasource="#$.site.getVar('datasource')#">
					update user
					set active = 1
					where user.id = #$.database.uuid(arguments.userID)#
				</cfquery>
				
				<cfset local.result = 1 /> <!--- activated --->
			</cfif>
		<cfelse>
			<cfset local.result = 3 /> <!--- invalid link --->
		</cfif>
		
		<cfreturn local.result />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="populateNewAccount" access="public" output="true" returntype="void">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.year = year(now()) />

		<!--- first goal --->
		<cfset local.goalID1 = $.run.it('goalService.saveGoal',{
			userID=arguments.userID,
			name="Reach 10,000 Facebook Likes",
			start="01/01/#local.year#",
			stop="12/31/#local.year#",
			description="This is a goal example"
		}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="01/01/#local.year#",description="Starting Point: 5,562 likes",complete=1,locked=-1}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="02/01/#local.year#",description="5,900 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="03/01/#local.year#",description="6,250 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="04/01/#local.year#",description="6,600 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="05/01/#local.year#",description="6,950 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="06/01/#local.year#",description="7,300 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="07/01/#local.year#",description="7,650 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="08/01/#local.year#",description="8,000 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="09/01/#local.year#",description="8,350 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="10/01/#local.year#",description="8,700 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="11/01/#local.year#",description="9,050 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="12/01/#local.year#",description="9,400 likes",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID1,date="12/31/#local.year#",description="10,000!!",complete=0,locked=1}) />
		
		<cfset local.actionID = $.run.it('goalSErvice.saveACtion',{goalID=local.goalID1,name="Collect Content"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID,name="Collect Content",duration=60,category=1,weekday=1,start_time="01/01/2014 09:00:00"}) />
		<cfset local.actionID2 = $.run.it('goalSErvice.saveAction',{goalID=local.goalID1,name="Share on Facebook"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID2,name="Share on Facebook",duration=15,category=1,weekday=1,start_time="01/01/2014 10:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID2,name="Share on Facebook",duration=15,category=1,weekday=3,start_time="01/01/2014 10:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID2,name="Share on Facebook",duration=15,category=1,weekday=5,start_time="01/01/2014 10:00:00"}) />
		<cfset local.actionID3 = $.run.it('goalSErvice.saveAction',{goalID=local.goalID1,name="Engage with Users"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID3,name="Engage with Users",duration=30,category=1,weekday=2,start_time="01/01/2014 16:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID3,name="Engage with Users",duration=30,category=1,weekday=4,start_time="01/01/2014 16:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID3,name="Engage with Users",duration=30,category=1,weekday=6,start_time="01/01/2014 16:00:00"}) />
		
		<!--- second goal --->
		<cfset local.goalID2 = $.run.it('goalService.saveGoal',{
			userID=arguments.userID,
			name="Reach $100,000 in revenue",
			start="01/01/#local.year#",
			stop="12/31/#local.year#",
			description="This is a goal example"
		}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID2,date="01/01/#local.year#",description="$0 Revenue",complete=1,locked=-1}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID2,date="04/01/#local.year#",description="$15,000",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID2,date="07/01/#local.year#",description="$35,000",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID2,date="10/01/#local.year#",description="$55,000",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID2,date="12/31/#local.year#",description="$100,000",complete=0,locked=1}) />
		
		<cfset local.goalID3 = $.run.it('goalService.saveGoal',{
			userID=arguments.userID,
			name="2015 Craft Show",
			start="01/01/#local.year#",
			stop="06/16/#local.year#",
			description="This is a goal example"
		}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID3,date="03/25/#local.year#",description="Registration Deadline",complete=1,locked=-1}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID3,date="04/01/#local.year#",description="50% inventory complete",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID3,date="06/01/#local.year#",description="75% inventory complete",complete=0,locked=0}) />
		<cfset $.run.it('goalService.saveMilestone',{goalID=local.goalID3,date="06/16/#local.year#",description="Craft show!",complete=0,locked=1}) />
		<cfset local.actionID = $.run.it('goalSErvice.saveAction',{goalID=local.goalID3,name="Craft Widgets"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID,name="Craft Widgets",duration=120,category=1,weekday=3,start_time="01/01/2014 13:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID,name="Craft Widgets",duration=120,category=1,weekday=4,start_time="01/01/2014 13:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,goalActionID=local.actionID,name="Craft Widgets",duration=120,category=1,weekday=5,start_time="01/01/2014 13:00:00"}) />
		
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=0,start_time="01/01/2014 12:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=1,start_time="01/01/2014 12:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=2,start_time="01/01/2014 12:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=3,start_time="01/01/2014 12:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=4,start_time="01/01/2014 12:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=5,start_time="01/01/2014 12:00:00"}) />
		<cfset $.run.it('workweekService.saveWorkweekEvent',{userID=arguments.userID,name="Lunch Break",duration=45,category=3,weekday=6,start_time="01/01/2014 12:00:00"}) />
		
		<cfset $.run.it('calendarService.saveEvent',{
			id="",
			description="Signed up for Craftivity!",
			start=$.date.getTime(),
			stop=$.date.getTime(),
			category=3,
			date=$.date.getDate(),
			userID=arguments.userID
		}) />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="signAgreement" access="public" output="true" returntype="string">
		
		<cfset var local = {} />

		<cfset $.database.save(
			table="user",
			id=request.session.user.id,
			agreement=1		
		) />
		
		<!--- invalidate cache on this user --->
		<cfset local.user = $.run.it('craftivityService.getUser',{userID=request.session.user.id,invalidateCache=true}) />
		
		<!--- update request with user values --->
		<cfset application.session.pushUserToSession(local.user) />
				
		<cfreturn 1 />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="terminateUser" access="public" output="true" returntype="string">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />

		<cfset $.database.delete(
			table="user",
			id=arguments.userID
		) />
				
		<cfreturn 1 />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveHopscotchStart" access="public" output="true" returntype="string">
		<cfargument name="tourKey" required="true" />
		
		<cfset var local = {} />

		<cfset $.run.it('optionService.saveOptions',{
			key="hopscotch",
			args={"#arguments.tourKey#"=true}
		}) />
				
		<cfreturn 1 />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="saveHopscotchFinish" access="public" output="true" returntype="string">
		<cfargument name="tourKey" required="true" />
		
		<cfset var local = {} />

		<cfset $.run.it('optionService.saveOptions',{
			key="hopscotch",
			args={"#arguments.tourKey#_fin"="#$.date.getDate()#"}
		}) />
				
		<cfreturn 1 />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="updateLastActivity" access="public" output="true" returntype="string">
		
		<cfset var local = {} />
		
		<cfif request.session.user.id neq "">
			<cfset application.helpers.database.save(
				id=request.session.user.id,
				table="user",
				last_request=dateTimeFormat(now(),'yyyy-mm-dd HH:nn:ss')
			) />
		</cfif>

		<cfreturn true />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="sendBetaEmails" access="public" output="true" returntype="string">
		
		<cfset var local = {} />

		<cfquery name="local.emails" datasource="#$.site.getVar('datasource')#">
			select 
			signup_email.id,
			signup_email.email,
			date_format(signup_email.created,'%m/%d/%Y %H:%i:%s') as created
			from signup_email 
			left join user on user.email = signup_email.email
				and user.active = 1
			where signup_email.active = 1
			and user.id is null
			order by signup_email.created desc
		</cfquery>
		
		<cfloop query="local.emails">
			<cfset local.result = $.run.it('emailService.sendOpenBetaSpotsEmail',{email="#local.emails.email#"}) />
		</cfloop>

		<cfreturn true />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="removeFromBetaList" access="public" output="true" returntype="string">
		<cfargument name="email" required="true" />
		
		<cfset var local = {} />

		<cfquery name="local.removeFromBetaList" datasource="#$.site.getVar('datasource')#">
			update signup_email
			set active = 0
			where email = #$.database.string(arguments.email)#
		</cfquery>

		<cfreturn true />
				
	</cffunction>
	
	<!--- --->
	
	<cffunction name="updateDST" access="public" output="true" returntype="string">
		<cfargument name="DST" required="true" />
		
		<cfset var local = {} />
		
		<cfset local.business = $.run.it('craftivityService.getBusiness',{userID=request.session.user.id}) />
		
		<cfif local.business.id eq "">
			<cfreturn />
		</cfif>
		
		<cfset $.database.save(
			table="business",
			id=local.business.id,
			dst=arguments.dst
		) />
		
		<cfset $.run.it('craftivityService.setLoggedInUser',{userID=request.session.user.id}) />
						
	</cffunction>
	
	<!--- --->
	
</cfcomponent>